package com.example.base.app.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * 基础Activity
 */
abstract class BaseActivity<VDB:ViewDataBinding> : AppCompatActivity() {
    protected lateinit var mDataBinding: VDB
    /**
     * 参数savedInstanceState用于获取Activity的实例状态
     * 场景：当Activity重新创建（屏幕旋转，配饰更新）
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //使用DataBinding设置布局
        mDataBinding = DataBindingUtil.setContentView(this,getLayoutId())
        //初始化UI
        initView()
    }

    protected abstract fun initView()

    protected abstract fun getLayoutId():Int

    /**
     * 重新获取Activity重建后的实例状态参数
     */
    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        savedInstanceState?.run {
            getActivityState(this)
        }
    }

    protected open fun getActivityState(state: Bundle){

    }
    /**
     * 保存当前Activity的状态
     * 屏幕旋转，配置更新
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        saveActivityState(outState)
    }

    protected open fun saveActivityState(outState: Bundle){

    }
}